package organization

import (
	"awesomeProject1/models/pojo"
	"awesomeProject1/pkg/e"
	"awesomeProject1/pkg/gintool"
	"github.com/simplechain-org/client/log"
	"xorm.io/xorm"
)

func GetOrganizationByID(s *xorm.Engine, orgID string, pager *gintool.Pager) ([]models.Organization, int64, int) {
	var record models.Organization
	var list []models.Organization
	sql := s.Table(record.TableName())
	if orgID != "" {
		sql = sql.Where("code = ?", orgID)
	} else {
		log.Error("机构编号参数为空：")
		return nil, -1, e.InvalidContentIsNull
	}
	count, err := sql.Limit(pager.PageSize, pager.NumStart).FindAndCount(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, count, e.ErrorDbGetFail
	}
	return list, count, e.Ok
}

func GetOrganizationByName(s *xorm.Engine, orgName string, pager *gintool.Pager) ([]models.Organization, int64, int) {
	var record models.Organization
	var list []models.Organization
	sql := s.Table(record.TableName())
	if orgName != "" {
		sql = sql.Where("org_name = ?", orgName)
	} else {
		log.Error("机构名称参数为空：")
		return nil, -1, e.ErrorDbGetFail
	}
	count, err := sql.Limit(pager.PageSize, pager.NumStart).FindAndCount(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, count, e.ErrorDbGetFail
	}
	return list, count, e.Ok
}

func ListOrganization(s *xorm.Engine, search string, pager *gintool.Pager) ([]models.Organization, int64, int) {
	var record models.Organization
	var list []models.Organization
	sql := s.Table(record.TableName())
	if search != "" {
		sql = sql.Where("org_name like ?", "%"+search+"%")
	}
	count, err := sql.Limit(pager.PageSize, pager.NumStart).FindAndCount(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, count, e.ErrorDbGetFail
	}
	return list, count, e.Ok
}

func ListOrganizationFromParentOrg(s *xorm.Engine, orgID string, pager *gintool.Pager) (models.OrganizationList, int64, int) {
	var record models.Organization
	var list models.OrganizationList
	sql := s.Table(record.TableName())
	if orgID != "" {
		sql = sql.Where("code = ?", orgID).Or("parent_code = ?", orgID)
	}
	count, err := sql.Limit(pager.PageSize, pager.NumStart).FindAndCount(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, count, e.ErrorDbGetFail
	}
	return list, count, e.Ok
}

func InsertOrganization(s *xorm.Engine, organization *models.Organization) int {
	n, err := s.InsertOne(organization)
	if err != nil {
		log.Error("数据库插入链错误：", "err", err.Error())
		return e.ErrorDbInsertFail
	}
	if n < 1 {
		log.Error("插入数据影响行数： 0")
		return e.ErrorDbRowsNoEffect
	}
	return e.Ok
}

func UpdateOrganization(s *xorm.Engine, organization *models.Organization, cols []string) (int64, int) {
	n, err := s.Where("code = ?", organization.Code).Cols(cols...).Update(organization)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return n, e.ErrorDbGetFail
	}
	return n, e.Ok
}

func getOrgNames(s *xorm.Engine, orgCodes, cols []string) (models.OrganizationList, int) {
	var record models.Organization
	var list models.OrganizationList
	err := s.Table(record.TableName()).Cols(cols...).In("code", orgCodes).Find(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, e.ErrorDbGetFail
	}
	return list, e.Ok
}

func getOrgNamesByParent(s *xorm.Engine, orgId string, cols []string) (models.OrganizationList, int) {
	var record models.Organization
	var list models.OrganizationList
	err := s.Table(record.TableName()).Cols(cols...).Where("code = ?", orgId).Or("parent_code = ?", orgId).Find(&list)
	if err != nil {
		log.Error("数据库获取数据错误：", "err", err.Error())
		return nil, e.ErrorDbGetFail
	}
	return list, e.Ok
}

func orgListByIds(db *xorm.Engine, ids []uint32) (models.OrganizationList, error) {
	var list models.OrganizationList
	err := db.In("id", ids).Find(&list)
	if err != nil {
		return nil, err
	}
	return list, err
}

func InsertOrgSession(s *xorm.Session, organization *models.Organization) error {
	_, err := s.Insert(organization)
	if err != nil {
		log.Error("数据库插入链错误：", "err", err.Error())
		return err
	}
	return nil
}
